package org.springblade.modules.restaurant.wrapper;

import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.restaurant.entity.Area;
import org.springblade.modules.restaurant.service.IAreaService;
import org.springblade.modules.restaurant.vo.AreaVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author harry
 * @公众号 Harry技术
 */
public class AreaWrapper extends BaseEntityWrapper<Area, AreaVO> {

	private static final IAreaService areaService;

	static {
		areaService = SpringUtil.getBean(IAreaService.class);
	}

	public static AreaWrapper build() {
		return new AreaWrapper();
	}

	@Override
	public AreaVO entityVO(Area entity) {
		AreaVO areaVO = BeanUtil.copyProperties(entity, AreaVO.class);

		if (Func.equals(entity.getParentId(), CommonConstant.TOP_PARENT_ID)) {
			areaVO.setParentName(CommonConstant.TOP_PARENT_NAME);
		} else {
			Area parent = areaService.getById(entity.getParentId());
			areaVO.setParentName(parent.getAreaName());
		}
		return areaVO;
	}

	public List<AreaVO> listNodeVO(List<Area> list) {
		List<AreaVO> collect = list.stream().map(this::entityVO).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}
}
